package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.IPaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Author YanJingren
 * @Date 2021/2/23
 * @Description com.atguigu.springcloud.controller
 */
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {

    @Resource
    private IPaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/getPaymentById/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        Payment result = paymentService.getPaymentById(id);
        if (result != null) {
            return new CommonResult<>(200, "查询成功, serverPort: " + serverPort, result);
        } else {
            return new CommonResult<>(445, "查询失败，没有对应记录，查询id为" + id);
        }

    }

    @PostMapping("/create")
    public CommonResult<Integer> create(@RequestBody Payment payment) {
        int count = paymentService.create(payment);
        log.info("********插入结果: " + count);
        if (count > 0) {
            return new CommonResult<>(200, "插入数据库成功, serverPort: " + serverPort, count);
        } else {
            return new CommonResult<>(444, "插入数据库失败", count);
        }
    }

    @GetMapping("/lb")
    public String getPaymentLB() {
        return serverPort;
    }

}
